package Org.lanqiao.team.service;

import Org.lanqiao.team.domain.Architect;
import Org.lanqiao.team.domain.Designer;
import Org.lanqiao.team.domain.Employee;
import Org.lanqiao.team.domain.Programmer;
import Org.lanqiao.team.service.TeamException;

import static Org.lanqiao.team.service.Status.*;

public class TeamService {

    private int counter = 1;
    final int MAX_MEMBER= 6;
    Programmer[] team= new Programmer[1];//团队成员
    private int total= 0;
    public static int programmerCount;//programmer count
    public static int architectCount;//architect count
    public static int designerCount;//designer count
    Status status;
    /**返回当前团队的所有对象
    返回：包含所有成员对象的数组，数组大小与成员人数一致*/
    public Programmer[] getTeam(){
        return team;
    }

    /**向团队中添加成员
    参数：待添加成员的对象
    异常：添加失败， TeamException 中包含了失败原因*/
    public void addMember(Employee e) {
        Programmer[] team2= new Programmer[team.length+1];//0  ?  0 1
        if(team.length>MAX_MEMBER-1) {TeamException teamException= new TeamException("添加失败，成员已满，无法添加");}
        if(e.status==VOCATION) {TeamException teamException=new TeamException("添加失败，该员正在休假，无法添加");}
        if(e.status==BUSY) {TeamException teamException=new TeamException("添加失败，该员工已是某团队成员");}
        if(snow(e)) {TeamException teamException=new TeamException("添加失败，该员工已在本开发团队中");}
        if(e instanceof Architect) {
            Architect a=(Architect) e;
            if(architectCount == 1){TeamException teamException=new TeamException("添加失败，团队中至多只能有一名架构师");}
            else{//添加
                for (int i = 0; i < team.length-1; i++) {
                    team2[i] = team[i];
                }
                team2[team.length-1]=a;
                team=team2;
                architectCount++;
                return;
            }
        }
        else if(e instanceof Designer) {
            Designer d=(Designer) e;
            if(designerCount == 2){TeamException teamException=new TeamException("添加失败，团队中至多只能有两名设计师");}
            else{//添加
                for (int i = 0; i < team.length-1; i++) {
                    team2[i] = team[i];
                }
                team2[team.length-1]=d;
                team=team2;
                designerCount++;
                return;
            }
        }
        else if(e instanceof Programmer) {
            Programmer p=(Programmer) e;
            if(programmerCount == 3){TeamException teamException=new TeamException("添加失败，团队中至多只能有三名程序员");}
            else{//添加
                for (int i = 0; i < team.length-1; i++) {
                    team2[i] = team[i];
                }
                team2[team.length-1]=p;
                team=team2;
                programmerCount++;
                return;
            }
        }
        else {TeamException teamException=new TeamException("添加失败，该成员不是开发人员，无法添加");}
    }

    //是否已经在本团队
    private boolean snow(Employee e) {

        for (int i = 0; i < team.length-1; i++){
            if (e.getId() == team[i].getId()) {
                return true;
            }
        }
        return false;
    }


    /**从团队中删除成员
    参数：待删除成员的 memberId
    异常：找不到指定 memberId 的员工，删除失败*/
    public void removeMember(int memberId) {
        Programmer[] team3= new Programmer[team.length-1];
        int c=0;
        for (int i = 0; i < team.length-1; i++) {
            if (team[i].getId() == memberId){
                c=i;
                break;
            }
            team3[i] = team[i];
        }
        for (int i = c; i < team.length-c-1; i++) {
            team3[i] = team[i+1];
        }
        team=team3;
    }
}
